package com.heima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.VmMapper;
import com.heima.pojo.*;
import com.heima.pojo.dto.VmQueryParamDto;
import com.heima.service.NodeService;
import com.heima.service.VmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class VmServiceImpl implements VmService {
    @Autowired
    private VmMapper vmMapper;

    @Override
    public Boolean addVM(VendingMachine vendingMachine) {
        log.info("vendingMachine中都有什么？{}",vendingMachine);
        vendingMachine.setCreateTime(LocalDateTime.now());
        vendingMachine.setUpdateTime(LocalDateTime.now());
        vendingMachine.setNode(vmMapper.selectNodeByNodeID(vendingMachine.getNodeId()));
        vendingMachine.setType(vmMapper.selectVMTypeByvmType(vendingMachine.getVmType()));
        vendingMachine.setCreateUserName(vmMapper.selectCreateName(vendingMachine.getCreateUserId()));
        vendingMachine.setVmStatus(0);
        vendingMachine.setLastSupplyTime(LocalDateTime.now());
        vendingMachine.setCityCode(vmMapper.selectCityCodeByNodeId(vendingMachine.getNodeId()));
        vendingMachine.setRegionId(vmMapper.selectNodeByNodeID(vendingMachine.getNodeId()).getRegionId());
        vendingMachine.setOwnerId(1);//不知
        vendingMachine.setOwnerName("1");//不知
        vmMapper.addVendingMachine(vendingMachine);
        return null;
    }

    @Override
    public PageBean selectVmList(VmQueryParamDto vmDto) {
        String inncerrCode = vmDto.getInnerCode();
        //分三步，1 查VendingMachine 集合 遍历集合 分别 查 node region
        List<VendingMachine> vendingMachineList = vmMapper.selectVendingMachine(inncerrCode);
        for (VendingMachine big : vendingMachineList) {
            log.info("每一个big是：{}",big);
            //添加node
            //查node 查 type 查 region
            log.info("每一个nodeID：{}",big.getNodeId());
            Node node = vmMapper.selectNodeByNodeID(big.getNodeId());
            log.info("每一个regionID：{}",big.getNodeId());
            Region region = vmMapper.selectRegionByRegionId(big.getNodeId());
            log.info("每一个businessID：{}",big.getBusinessId());
            Business business = vmMapper.selectBusinessById(big.getBusinessId());
            log.info("每一个vmtypeID：{}",big.getVmType());
            Type type = vmMapper.selectVMTypeByvmType(big.getVmType());
            //(小)加region
            node.setRegion(region);
            //(小)加business
            node.setBusinessType(business);
            //(大)加type 加node 加region
            big.setNode(node);
            big.setType(type);
            big.setRegion(region);
        }
        PageHelper.startPage(vmDto.getPageIndex(), vmDto.getPageSize());
        PageInfo<VendingMachine> info = new PageInfo<>(vendingMachineList);


        return new PageBean(vmDto.getPageIndex(), vmDto.getPageSize(), (long) info.getPages(),info.getTotal(),info.getList());
    }
}
